﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace z.v.sub {
	/// <summary>
	/// Interaction logic for AutoComplete.xaml
	/// </summary>
	public partial class AutoComplete: UserControl {
		public bool IsChanged = false;
		public static readonly DependencyProperty SingleP = DependencyProperty.Register("Single", typeof(bool), typeof(AutoComplete));
		public bool Single { get { return (bool)GetValue(SingleP); } set { SetValue(SingleP, value); } }
		public static readonly DependencyProperty ClassNameP = DependencyProperty.Register("ClassName", typeof(string), typeof(AutoComplete));
		public string ClassName { get { return (string)GetValue(ClassNameP); } set { SetValue(ClassNameP, value); } }

		public Type zType;
		public AutoComplete() {
			InitializeComponent();
			lbx.DisplayMemberPath = "FullText";
		}

		void txt_LostFocus(object sender, RoutedEventArgs e) {
			pop.IsOpen = false;
		}
		private void _this_Loaded(object sender, RoutedEventArgs e) {
			//e.Handled = true;
			zType = Type.GetType("z.m." + ClassName);
			if (Tag is m.IText) {
				txt.Text = (Tag as m.IText).ToString();
			}
			if (Single) {
				txt.LostFocus += txt_LostFocus;
				btn_Clear.Visibility = Visibility.Collapsed;
			} else {
				txt.Focus();
			}
			txt.Width = _this.ActualWidth - btn_Clear.ActualWidth;
		}
		private void txt_GotFocus(object sender, RoutedEventArgs e) {
			lbx.Width = this.ActualWidth;
			pop.IsOpen = true;
			lbx.ItemsSource = (IQueryable)zType.InvokeMember("Search", System.Reflection.BindingFlags.InvokeMethod, null, null,
				new Object[] { "" });
			if (Tag != null) {
				txt.Text = (Tag as m.IText).FullText;
				lbx.SelectedItem = Tag;
				txt.SelectAll();
			}
			e.Handled = true;
		}
		private void lbx_SelectionChanged(object sender, SelectionChangedEventArgs e) {
			Tag = lbx.SelectedItem;
			if (lbx.SelectedIndex == -1)
				lbx.SelectedIndex = 0;
			if(lbx.SelectedItem!=null)
				Tag = lbx.SelectedItem;
			if (!txt.IsFocused && Tag is m.IText)
				txt.Text = (Tag as m.IText).ToString();
			IsChanged = true;
		}
		private void _this_KeyUp(object sender, KeyEventArgs e) {
			if (e.Key.Equals(Key.Up)) {
				lbx.SelectedIndex -= lbx.SelectedIndex > 0 ? 1 : 0;
			} else if (e.Key.Equals(Key.Down)) {
				lbx.SelectedIndex += lbx.SelectedIndex < lbx.Items.Count ? 1 : 0;
			}
		}

		private void pop_Closed(object sender, EventArgs e) {
			if (lbx.SelectedItem != null)
				Tag = lbx.SelectedItem;
			if (this.TemplatedParent is DataGrid) {
				(this.TemplatedParent as DataGrid).CommitEdit(DataGridEditingUnit.Cell,true);
			}
		}

		private void txt_TextChanged(object sender, TextChangedEventArgs e) {
			if (txt.IsFocused) {
				lbx.ItemsSource = (IQueryable)zType.InvokeMember("Search", System.Reflection.BindingFlags.InvokeMethod, null, null,
					new Object[] { txt.Text });
				if (lbx.SelectedIndex == -1)
					lbx.SelectedIndex = 0;
				IsChanged = true;
			}
		}

		private void Button_Click_1(object sender, RoutedEventArgs e) {
			e.Handled = true;
			lbx.SelectedIndex = -1;
			Tag = null;
			txt.Text = "";
		}

		private void txt_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
			if (e.KeyboardDevice.IsKeyDown(Key.Tab)&&IsChanged) {
				if (lbx.SelectedIndex == -1)
					lbx.SelectedIndex = 0;
				Tag = lbx.SelectedItem;
				if (Tag is m.IText)
					txt.Text = (Tag as m.IText).ToString();
			}
		}
	}
}
